package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.ChecksetmealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.ChecksetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 *
 * @author sot
 * @description 检查组service实现类
 **/
@Slf4j
@Service
public class ChecksetmealServiceImpl implements ChecksetmealService {

    @Autowired
    private ChecksetmealMapper checksetmealMapper;


    /**
     * 检查套餐分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult getInquire(QueryPageBean queryPageBean) {

        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //查询套餐数据
        Page<Setmeal> page = checksetmealMapper.getsetmealDATA(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 查询所有新增套餐列表的检查组
     * @param
     * @return
     */
    @Override
    public List<CheckGroup> insetSetmeal() {
        //调用mapper层进行新增
        List<CheckGroup> checkGroup = checksetmealMapper.serlactDATA();


        return checkGroup;
    }

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    @Transactional
    public void newlySetmeal(Setmeal setmeal,List<Integer> checkgroupIds) {
        //添加套餐并且返回主键id
        checksetmealMapper.setnewlySetmeal(setmeal);

        if(!CollectionUtils.isEmpty(checkgroupIds)){  //讲对应的检查组与检查套餐id进行关联
            for (Integer checkgroupId : checkgroupIds) {
                checksetmealMapper.relevance(setmeal.getId(),checkgroupId);

            }
        }

    }
}
